﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Web.Pages.Evaluation_Setup;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Web.Pages.Evaluation_Setup.Personalist;

namespace BSCMFF.Web.UserControls
{
    public partial class EvaluationQuestionCategory : System.Web.UI.UserControl
    {
        public bool IsEditMode { get; set; }

        public Panel PublicPanelQuestions 
        { 
            get
            {
                return PanelQuestions;
            }
        }

        private List<Evaluation_Question> savedQuestions;
        public List<Evaluation_Question> SavedQuestions 
        { 
            get
            {
                return savedQuestions;
            }
            set
            {
                savedQuestions = value;
            }
        }

        private int _numberQuestions;
        public int NumberQuestions
        {
            get
            {
                return (int)RadNumericTextBoxNumberOfQuestions.Value;
            }
            set
            {
                _numberQuestions = value;
            }
        }

        private int _min;
        public int Min 
        { 
            get
            {
                return (int)RadNumericTextBoxMin.Value;
            }
            set
            {
                _min = value;
            }
        }

        private int _max;
        public int Max
        {
            get
            {
                return (int)RadNumericTextBoxMax.Value;
            }
            set
            {
                _max = value;
            }
        }

        public event EventHandler EventAddCategory;
        //public EvaluationQuestions PublicEQ
        //{
        //    get
        //    {
        //        return EQ;
        //    }
        //}

        public string CategoryName
        {
            get
            {
                return RadTextBoxCategory.Text;
            }
            set
            {
                RadTextBoxCategory.Text = value;
            }
        }

        private static bool create = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (SavedQuestions != null)
            {
                SetQuestionsProperties(SavedQuestions.Count, Min, Max);
                create = true;
                CreateQuestion();

                int i = 0;

                //if (IsEditMode)
                //{
                    foreach (Evaluation_Question equestion in SavedQuestions)
                    {
                        RadTextBox rquestion = FindControl("Question" + i.ToString()) as RadTextBox;
                        rquestion.Text = equestion.Question;
                        ++i;
                    }
               /* }
                else
                {
                    foreach (Evaluation_Question equestion in SavedQuestions)
                    {//hide textboxes and show labels
                        RadTextBox rquestion = FindControl("Question" + i.ToString()) as RadTextBox;
                        Label lquestion = FindControl("LabelQuestion" + i.ToString()) as Label;
                        rquestion.Visible = false; 
                        lquestion.Visible = true;
                        lquestion.Text = equestion.Question;
                        ++i;
                    }

                    RadTextBoxCategory.Visible = false;
                    LabelCurrentCategory.Text = RadTextBoxCategory.Text;
                    LabelCurrentCategory.Visible = true;
                    PlaceHolderCategoryProperties.Visible = false;
                    
                }*/
                SavedQuestions = null;
            }
            //if (IsPostBack && PanelQuestions.Controls.Count == 1)
            //{
            else
            {
                create = true;
                //if ((sender.GetType().Name != typeof(RadButton).Name))
                //{
                CreateQuestion(); // ButtonCreateQuestions_Click2(sender, e);
                //}
            }
            //}
        }

        public void RadButtonUpdateQuestions_Click(object sender, EventArgs e)
        {
            if (sender.GetType().Name != "RadButton")
                return;

            int count = ((EvaluationEditMode)this.Page).Categories;

            for (int i = 0; i < count; ++i)
            {   //EvaluationQuestionCategory0
                //if (((RadButton)sender).ClientID == "ctl00_MainContent_EQC" + i.ToString() + "_RadButtonUpdateQuestions")
                if (((RadButton)sender).ClientID == "ctl00_MainContent_EvaluationQuestionCategory" + i.ToString() + "_RadButtonUpdateQuestions")
                {
                    Control ParentControl = ((EvaluationQuestionCategory)(((RadButton)sender).Parent.Parent.Parent)).PanelEvalBSC.FindControl("PanelQuestions");
                    Control c = ParentControl.FindControl("TableQuestions");
                    //((RadButton)sender).Parent.FindControl("ctl00_MainContent_EQC0_RadButtonUpdateQuestions");
                    ParentControl.Controls.Remove(c);
                    create = true;
                    CreateQuestion();
                }
            }
        }


        public void CreateQuestion()//Load(int number, int min, int max)
        {
            //RadNumericTextBoxMin.Value = (double?)min;
            //RadNumericTextBoxMax.Value = (double?)max;
            //RadNumericTextBoxNumberOfQuestions.Value = (double?)number;

            if (create == true && this.FindControl("TableQuestions") == null)
            {
                Table t = new Table();
                t.ID = "TableQuestions";

                Label l = new Label();
                l.ID = "LabelHeader";
                l.Text = "Questions";

                TableRow tr = new TableRow();
                TableCell tc = new TableCell();
                tc.Controls.Add(l);

                tr.Cells.Add(tc);



                for (int j = (int)RadNumericTextBoxMin.Value; j <= (int)RadNumericTextBoxMax.Value; ++j)
                {
                    Label ln = new Label();
                    ln.ID = "LabelNumber" + j.ToString();
                    ln.Text = j.ToString();

                    TableCell tcln = new TableCell();
                    tcln.Controls.Add(ln);
                    tcln.HorizontalAlign = HorizontalAlign.Center;
                    tr.Cells.Add(tcln);
                }



                t.Rows.Add(tr);

                //currentNumberOfQuestions = (int)RadNumericTextBoxNumberOfQuestions.Value;

                for (int i = 0; i < RadNumericTextBoxNumberOfQuestions.Value; ++i)
                {
                    RadTextBox q = new RadTextBox();
                    Label lq = new Label();

                    q.ID = "Question" + i.ToString();
                    lq.ID = "LabelQuestion" + i.ToString();
                    lq.Visible = false;
                    q.Width = 500;

                    TableRow trQ = new TableRow();
                    TableCell tcQ = new TableCell();
                    tcQ.Controls.Add(q);
                    tcQ.Controls.Add(lq);

                    trQ.Cells.Add(tcQ);

                    //PanelQuestions.Controls.Add(q);

                    //PanelQuestions.Controls.Add(new LiteralControl("<span>"));


                    for (int j = (int)RadNumericTextBoxMin.Value; j <= (int)RadNumericTextBoxMax.Value; ++j)
                    {
                        RadioButton rb = new RadioButton();

                        string si = i < 10 ? "0" + i.ToString() : i.ToString();
                        string sj = j < 10 ? "0" + j.ToString() : j.ToString();

                        rb.ID = "Radio" + si + sj;
                        rb.GroupName = "Group" + i.ToString();
                        rb.Enabled = false;
                        //PanelQuestions.Controls.Add(rb);

                        TableCell tcCH = new TableCell();
                        tcCH.Controls.Add(rb);

                        trQ.Cells.Add(tcCH);
                    }

                    //PanelQuestions.Controls.Add(new LiteralControl("</span>"));
                    //PlaceHolder ph = new PlaceHolder();

                    //ph.ID = "PlaceHolder" + i.ToString();

                    //PanelQuestions.Controls.Add(ph);

                    //PanelQuestions.Controls.Add(new LiteralControl("<br />"));
                    // trQ.Cells.Add(tcCH);
                    t.Rows.Add(trQ);
                }

                PanelQuestions.Controls.Add(t);
            }
            create = false;

        }

        protected void RadButtonCreateCategory_Click(object sender, EventArgs e)
        {
            //EQ.Visible = true;
            //RadButtonCreateCategory.Visible = false;

            //RadButtonAddCategory.Visible = true;
        }

        public void SetQuestionsProperties(int number, int min, int max)
        {
            create = true;
            RadNumericTextBoxMin.Value = (double?)min;
            RadNumericTextBoxMax.Value = (double?)max;
            RadNumericTextBoxNumberOfQuestions.Value = (double?)number;
        }
    }
}